package com.dushibao.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dushibao.admin.model.Admin;
import com.dushibao.admin.model.AdminMenu;
import com.dushibao.admin.model.Menu;
import com.dushibao.admin.mv.DushibaoModelAndView;
import com.dushibao.admin.service.IAdminMenuService;
import com.dushibao.admin.service.IAdminService;
import com.dushibao.admin.service.IMenuService;
import com.dushibao.admin.vo.State;
import com.dushibao.admin.vo.TreeData;
import com.dushibao.admin.vo.request.AdminModelPageRequest;
import com.dushibao.admin.vo.request.MenuModelPageRequest;
import com.dushibao.utils.IpUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * Title: Admin.java
 *
 * Description:用户信息 前端控制器
 *
 * Copyright: Copyright: Copyright (c) 2021
 *
 * Company: www.dushibao.com
 *
 * @author zqd
 *
 * @date 2021-12-06 08:28:09
 *
 * @version dushibao V1.0
 */
@Controller
public class AdminController {

    @Autowired
    IAdminService adminService;

    @Autowired
    IMenuService menuService;

    @RequestMapping("/admin/list")
    public ModelAndView list(AdminModelPageRequest modelPageRequest){
        ModelAndView mv = new DushibaoModelAndView("/admin/list",modelPageRequest);

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();

        if(modelPageRequest.getUserName()!=null){
            queryWrapper.like("userName",modelPageRequest.getUserName());
        }
        if(modelPageRequest.getTrueName()!=null){
            queryWrapper.like("trueName",modelPageRequest.getTrueName());
        }
        if(modelPageRequest.getTelephone()!=null){
            queryWrapper.like("telephone",modelPageRequest.getTelephone());
        }
        if(modelPageRequest.getSex()!=null && modelPageRequest.getSex()!=-2){
            queryWrapper.eq("sex",modelPageRequest.getSex());
        }

        IPage<Admin> page = new Page<>(modelPageRequest.getCurrent(),modelPageRequest.getPageSize());
        IPage<Admin> iPage = this.adminService.page(page,queryWrapper);

        mv.addObject("iPage",iPage);

        return mv;
    }

    @RequestMapping(value = "/admin/del",method = RequestMethod.POST)
    @ResponseBody
    public Boolean del(@RequestBody List<Long> ids){

        this.adminService.removeByIds(ids);

        return true;
    }

    @RequestMapping("/admin/add")
    public ModelAndView add(Long id){

        ModelAndView mv = new DushibaoModelAndView("/admin/add");

        if(id == null){
            return mv;
        }

        Admin obj = this.adminService.getById(id);

        mv.addObject("obj",obj);

        return mv;
    }

    @Autowired
    IAdminMenuService adminMenuService;

    @RequestMapping("/admin/save")
    public String save(Admin admin,@RequestParam List<Long> menuIds){
        this.adminService.saveOrUpdate(admin);

        if(menuIds!=null && menuIds.size()>0){
            //1、删除原有的关系
            QueryWrapper<AdminMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("admin_id",admin.getId());
            adminMenuService.remove(queryWrapper);
            //2、建立新的关系
            for (Long menuId : menuIds) {
                AdminMenu adminMenu = new AdminMenu();
                adminMenu.setAdminId(admin.getId());
                adminMenu.setMenuId(menuId);

                this.adminMenuService.save(adminMenu);

            }
        }

        return "redirect:/admin/list";
    }

    @RequestMapping(value = "/admin/validator",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> validator(Admin admin){
        Map<String,Object> ret = new HashMap<>();

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userName",admin.getUserName());
        List<Admin> list = this.adminService.list(queryWrapper);

        if(list == null || list.size() == 0){
            ret.put("code",10000);
            ret.put("msg","验证通过");
        }else{
            ret.put("code",10001);
            ret.put("msg","用户名重复");
        }

        return ret;
    }

    @RequestMapping("/admin/treeMenu")
    @ResponseBody
    public List<TreeData> treeMenu(Long adminId){
        List<TreeData> firstTree = new ArrayList<>();

        //管理员拥有的菜单
        List<Menu> menus = this.menuService.findAdminMenus(adminId);

        List<Long> menuIds = menus.stream().map(Menu::getId).collect(Collectors.toList());

        //1、第一层
        List<Menu> parentMenus = this.menuService.findTreeMenu();
        for (Menu parentMenu : parentMenus) {
            TreeData firstData = new TreeData();
            firstData.setId(parentMenu.getId());
            firstData.setText(parentMenu.getName());

            if(menuIds!=null && menuIds.contains(firstData.getId())){
                State state = new State();
                state.setSelected(true);

                firstData.setState(state);
            }

            //2、第二层
            for (Menu child : parentMenu.getChilds()) {
                TreeData secondData = new TreeData();
                secondData.setId(child.getId());
                secondData.setText(child.getName());

                firstData.getChildren().add(secondData);

                if(menuIds!=null && menuIds.contains(secondData.getId())){
                    State state = new State();
                    state.setSelected(true);
                    secondData.setState(state);
                }


            }

            firstTree.add(firstData);
        }

        return firstTree;
    }

}
